package com.shanghaionstar.login.presenter;

import android.content.Context;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.joinme.RegisterToVisitorResponse;
import com.shanghaionstar.R;
import com.shanghaionstar.login.SupplementPhoneActivity;
import com.shanghaionstar.login.iview.ISupplePhoneView;
import com.shanghaionstar.user.service.ChangeEmailOrPhoneService;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.weight.CommonInputLayout;

/**
 * Created by 6335yz on 2016/6/28.
 */
public class SupplementPhonePresenter extends BasePresenter<RegisterToVisitorResponse> {
    private Context context;
    private ISupplePhoneView supplePhoneView;
    private ChangeEmailOrPhoneService service;

    public SupplementPhonePresenter(IMVPScheduler mvpScheduler, Context context) {
        super(mvpScheduler);
        this.context = context;
    }

    @Override
    public void setView(IMVPView<RegisterToVisitorResponse> view) {
        super.setView(view);
        supplePhoneView = (ISupplePhoneView) view;
        service = new ChangeEmailOrPhoneService(view.onActivity());
        if (context instanceof SupplementPhoneActivity) {
            String gaaPhoneNumber = ((SupplementPhoneActivity) context).getIntent().getStringExtra("gaaPhoneNumber");
            supplePhoneView.showGaaPhoneNumber(gaaPhoneNumber);
        }
        super.setServiceProvider(service);
    }

    public boolean verifyInput(CommonInputLayout inputLayout) {
        if (CommonUtil.isNull(inputLayout.getText())) {
            supplePhoneView.verifyResult(inputLayout, R.string.input_user_info_not_null);
            return false;
        }

        if (!CommonUtil.isPhone(inputLayout.getText())) {
            supplePhoneView.verifyResult(inputLayout, R.string.new_phone_error);
            return false;
        }
        supplePhoneView.verifyResult(inputLayout, FinalConstants.Register.NO_ERROR);
        return true;
    }

    public boolean verifyInputCode(CommonInputLayout inputLayout) {
        if (CommonUtil.isNull(inputLayout.getText())) {
            supplePhoneView.verifyResult(inputLayout, R.string.input_user_info_not_null);
            return false;
        }
        supplePhoneView.verifyResult(inputLayout, FinalConstants.Register.NO_ERROR);
        return true;
    }

    public void setUserInfo(String newPhoneNumber, String verifyCode) {
        service.setUserInfo("",newPhoneNumber, verifyCode);
    }


}
